package com.boot.system.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.boot.framework.aop.Log;
import com.boot.framework.common.R;
import com.boot.system.entity.SysOperateLogEntity;
import com.boot.system.service.ISysOperateLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 操作日志记录
 *
 * @author pig
 * @date 2024-03-09 14:35:02
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/operateLog")
@Tag(name = "操作日志")
public class SysOperateLogController {

    private final ISysOperateLogService sysOperateLogService;

    /**
     * 分页查询
     *
     * @return
     */

    @GetMapping("/page")
    @Operation(summary = "分页")
    public R page(@RequestParam Map<String, Object> params) {
        return R.ok(sysOperateLogService.selectPage(params));
    }


    /**
     * 通过id查询操作日志记录
     *
     * @param id id
     * @return R
     */

    @GetMapping("/{id}")
    @Operation(summary = "查看")
    public R view(@PathVariable("id") String id) {
        return R.ok(sysOperateLogService.getById(id));
    }

    /**
     * 新增操作日志记录
     *
     * @param sysOperateLog 操作日志记录
     * @return R
     */

    @PostMapping
    @Operation(summary = "新增")
    public R save(@RequestBody SysOperateLogEntity sysOperateLog) {
        return R.ok(sysOperateLogService.save(sysOperateLog));
    }

    /**
     * 修改操作日志记录
     *
     * @param sysOperateLog 操作日志记录
     * @return R
     */

    @PutMapping
    @Operation(summary = "修改")
    public R update(@RequestBody SysOperateLogEntity sysOperateLog) {
        return R.ok(sysOperateLogService.updateById(sysOperateLog));
    }

    /**
     * 通过id删除操作日志记录
     *
     * @param ids id列表
     * @return R
     */

    @DeleteMapping
    @Log(title = "日志管理", content = "删除")
    @Operation(summary = "删除")
    public R delete(@RequestBody String[] ids) {
        return R.ok(sysOperateLogService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param sysOperateLog 查询条件
     * @param ids           导出指定ID
     * @return excel 文件流
     */
    @GetMapping("/export")
    @Operation(summary = "导出")
    public List<SysOperateLogEntity> export(SysOperateLogEntity sysOperateLog, String[] ids) {
        return sysOperateLogService.list(Wrappers.lambdaQuery(sysOperateLog).in(ArrayUtil.isNotEmpty(ids), SysOperateLogEntity::getId, ids));
    }
}